package com.kube.gateway.core.router.manager;

import com.kube.gateway.core.router.domain.dto.RouteDefinitionDto;
import com.kube.gateway.core.router.domain.entity.RouterEntity;
import com.kube.gateway.core.router.domain.mapper.RouterMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class RouterManager {
    @Autowired
    private RouterMapper routerMapper;

    public Boolean saveRoute(RouteDefinitionDto routeDefinitionDto) {
        Boolean update = Boolean.FALSE;
        if (routeDefinitionDto.getId() != null) {
            RouterEntity routerEntity = routerMapper.getById(routeDefinitionDto.getId());
            update = routerEntity != null;
        }
        if (update) {
            routerMapper.update(routeDefinitionDto.toEntity());
        } else {
            routerMapper.save(routeDefinitionDto.toEntity());
        }
        return Boolean.TRUE;
    }

    public Boolean delRoute(Long id, Long updateId) {
        RouterEntity routerEntity = new RouterEntity();
        routerEntity.setId(id);
        routerEntity.setUpdateId(updateId);
        return routerMapper.update(routerEntity) > 0;
    }
}
